<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Hosparticletype extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public $allTypeLists = array();

    public function _initialize()
    {
        $this->_model = new \app\admin\model\HospArticleType();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);

        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);

        //获取文章大的分类
        $bigTypeLists = getHospBigTypeArticle();
        $this->assign('bigTypeLists', changeKeyForList($bigTypeLists,'id'));

    }


    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }
    public function ajaxData()
    {

        $check_id = trim(input("request.check_id"));//auth
        if(empty($check_id))
        {
            $check_id=0;
        }
        $this->allTypeLists = getAllHospArticleType();
        $this->allTypeLists = makeTreeSelectData($this->allTypeLists,$check_id);
        return json($this->allTypeLists);
    }

    public function lists()
    {
        $where = array();
        $lists = $this->_model->getDataForWheres($where, "id,name,sorts,pid as pId", "pId asc,sorts asc");
        foreach ($lists as $key => $val) {
            //if ($val['pId'] == 0) {
            $lists[$key]['open'] = true;//一级展开
            $lists[$key]['icon'] = '';//自定义图标
            //}

        }
        $this->assign('lists', $lists);
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $child = $this->_model->getDataForWhere(array('pid' => $id));
        if (!empty($child)) {
            $return = ['err' => 3, 'msg' => '操作失败,该分类下还有子分类，暂不能删除'];
            return json($return);
        }
        $res = $this->_model->delData(array('id'=>$id));
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    /**
     * 分类排序
     * @return \think\response\Json
     */
    public function sorts()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        $sorts = trim(input("request.sorts"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if (empty($sorts)) {
            $sorts = 0;
        }
        $model = new \app\admin\model\NotesFiles();
        $res = $model->updateData(array('sorts' => $sorts), $id);
        if ($res || $res == 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    public function getMaxSorts($pid)
    {
        $model = new \app\admin\model\HospArticleType();
        $res = $model->getDataForWheres(array('pid' => $pid), "*", "sorts desc", 1);
        $jump = 10;
        if ($res) {
            $jump = $jump + $res[0]['sorts'];
        }
        return $jump;
    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {

            $name = $_REQUEST['name'];
            $pid = $_REQUEST['pid'];
            if (empty($pid)) {
                $pid = 0;
            }
            //获取最大的排序
            $sorts = $this->getMaxSorts($pid);
            $data = array(
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time(),
                'sorts' => $sorts,
                'pid' => $pid,
                'name' => $name,
            );
            $res = $this->_model->add($data);
            if ($res) {
                $data['id'] = $res;
                $data['pId'] = $data['pid'];
                $return = ['err' => 0, 'msg' => '操作成功', 'data' => $data];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $name = $_REQUEST['name'];
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if (empty($info)) {
            $return = ['err' => 3, 'msg' => '操作失败,分类不存在'];
            return json($return);
        }
        if (empty($name)) {
            $return = ['err' => 3, 'msg' => '操作失败,分类名称不能为空'];
            return json($return);
        }


        $w = array(
            'id' => $id
        );
        $data = array(
            'name' => $name,
        );

        $res = $this->_model->updateDataForWhere($data, $w);

        if ($res || ($res === 0)) {

            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }
}